import hashlib
from time import time

from flask_restful import Resource,reqparse
from flask import request, current_app

from . import product_api,product_bp
from  .. import models,db

class Products(Resource):
    def post(self):
        '''添加商品'''
        try:
            parser = reqparse.RequestParser()
            #添加参数
            parser.add_argument('name',type=str,required=True,help='请输入商品名称')
            parser.add_argument('price',type=float,help='请输入商品价格')
            parser.add_argument('number',type=int,help='请输入商品数量')
            parser.add_argument('introduce',type=str,help='请输入商品介绍')
            parser.add_argument('weight',type=int,help='请输入商品重量')
            parser.add_argument('cid_one',type=int,required=True,help='请选择一级分类')
            parser.add_argument('cid_two',type=int,required=True,help='请选择二级分类')
            parser.add_argument('cid_three',type=int,required=True,help='请选择三级分类')

            parser.add_argument('pics',type=list,location='json')
            parser.add_argument('attr_static',type=list,location='json')
            parser.add_argument('attr_dynamic',type=list,location='json')
            #解析参数
            args = parser.parse_args()
            #创建商品对象
            product = models.Product(name=args.get('name'),price=args.get('price'),number=args.get('number'),introduce=args.get('introduce'),weight=args.get('weight'),cid_one=args.get('cid_one'),cid_two=args.get('cid_two'),cid_three=args.get('cid_three'))
            #添加商品
            db.session.add(product)
            db.session.commit()

            #增加商品图片
            for p in args.get('pics'):
                pic = models.Picture(path=p.get('path'),pid=product.id)
                db.session.add(pic)
            #增加商品静态属性
            for a in args.get('attr_static'):
                attr = models.ProductAttr(pid=product.id,aid=a.get('id'),val=a.get('val'),_type='static')
                db.session.add(attr)
            #增加商品动态属性
            for a in args.get('attr_dynamic'):
                attr = models.ProductAttr(pid=product.id,aid=a.get('id'),val=','.join(a.get('val')),_type='dynamic')
            db.session.commit()
            return {'status':200,'msg':'添加商品成功'}
        except Exception as e:
            return {'status':500,'msg':'添加商品失败'}

    def get(self):
        '''获取商品列表'''
        #创建参数解析器
        parser = reqparse.RequestParser()
        #添加参数
        parser.add_argument('name',type=str,location='args')
        #解析参数
        args = parser.parse_args()
        #获取参数
        name = args.get('name')
        if name:
            product_list = models.Product.query.filter(models.Product.name.like( f'%{name}%')).all()
        else:
            product_list = models.Product.query.all()
        return {'status':200,'msg':'获取商品列表成功','data':[product.to_dict() for product in product_list]}

product_api.add_resource(Products,'/products/')

class Product(Resource):
    def delete(self,id):
        '''删除商品'''
        try:
            product = models.Product.query.get(id)
            db.session.delete(product)
            db.session.commit()
            return {'status':200,'msg':'删除商品成功'}
        except Exception as e:
            return {'status':500,'msg':'删除商品失败'}

product_api.add_resource(Product,'/product/<int:id>/')

@product_bp.route('/upload_img/',methods=['POST'])
def upload_img():
    '''上传图片'''
    #获取图片
    img_file = request.files.get('file')
    #判断图片是否存在
    if not img_file:
        return {'status':500,'msg':'图片不存在'}
    #判断图片的类型是否是允许上传的类型
    if allowed_file(img_file.filename):
        #获取图片名
        file_name = md5_file() + '.' + img_file.filename.rsplit('.',1)[-1]
        #保存图片
        img_file.save(current_app.config.get('UPLOAD_DIR') + '/' + file_name)
        #封装数据
        data = {
            'path':f'/static/uploads/{file_name}',
            'url':f'http://127.0.0.1:5000/static/uploads/{file_name}'

        }
        return {'status':200,'msg':'上传成功','data':data}
    else:
        return {'status':500,'msg':'图片类型不允许'}

def allowed_file(filename):
    '''判断文件名是否为允许的类型'''
    # if '.' in filename:
    #     #获取文件名后缀
    #     suffix = filename.rsplit('.',1)[-1]
    #     if suffix in current_app.config.ALLOWED_EXTENSIONS:
    #         return True
    #     return False
    # else:
    #     return False
    return '.' in filename and filename.rsplit('.',1)[-1] in current_app.config.get('ALLOWED_EXTENSIONS')

def md5_file():
    '''通过md5算法，对时间戳加密，返回字符串'''
    #创建hashlib对象
    md5 = hashlib.md5()
    #获取时间戳
    timestamp = str(time())
    #对时间戳进行加密
    md5.update(timestamp.encode())
    #获取加密后的字符串
    file_name = md5.hexdigest()
    return file_name